package com.nxk.modules.core.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nxk.modules.core.entity.BaseEntity;
import com.nxk.modules.core.entity.SysParam;
import com.nxk.modules.core.service.SysParamService;
import com.nxk.modules.exception.ResultEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/a/core/param")
public class SysParamController {
    
    @Autowired
    private SysParamService sysParamService;
    
    
    /**
     * 保存数据接口
     * @param sysParam
     * @return
     */
    @RequiresPermissions("core:param:edit")
    @PutMapping("/save")
    public ResultEntity save(@Validated SysParam sysParam) {
        QueryWrapper<SysParam> eq = new QueryWrapper<SysParam>()
                .eq("param_key", sysParam.getParamKey())
                .in("status", BaseEntity.STATUS_NORMAL,BaseEntity.STATUS_STOP);
        if (sysParamService.getOne(eq) == null) {
            boolean save = sysParamService.save(sysParam);
            return save ? ResultEntity.success(sysParamService.getById(sysParam.getId())) : ResultEntity.fail();
        }
        return ResultEntity.fail("此参数键名已存在");
    }
    
    
    /**
     * 修改数据状态接口
     * @param sysParam
     * @return
     */
    @RequiresPermissions("core:param:edit")
    @PostMapping("/updateStatus")
    public ResultEntity updateStatus(SysParam sysParam) {
        boolean save = sysParamService.save(sysParam);
        return save ? ResultEntity.success(sysParamService.getById(sysParam.getId())) : ResultEntity.fail();
    }
    
    
    /**
     * 更新数据接口
     * @param sysParam
     * @return
     */
    @RequiresPermissions("core:param:edit")
    @PostMapping("/update")
    public ResultEntity update(@Validated SysParam sysParam) {
        QueryWrapper<SysParam> eq = new QueryWrapper<SysParam>()
                .eq("param_key", sysParam.getParamKey())
                .notIn("id",sysParam.getId())
                .in("status", BaseEntity.STATUS_NORMAL,BaseEntity.STATUS_STOP);
        if (sysParamService.getOne(eq) == null) {
            boolean save = sysParamService.save(sysParam);
            return save ? ResultEntity.success(sysParamService.getById(sysParam.getId())) : ResultEntity.fail();
        }
        return ResultEntity.fail("此参数键名已存在");
    }
    
    
    /**
     * 根据id获取数据接口
     * @param sysParam
     * @return
     */
    @RequiresPermissions("core:param:view")
    @GetMapping("/get")
    public ResultEntity get(SysParam sysParam) {
        return ResultEntity.success(sysParamService.getById(sysParam.getId()));
    }



    @GetMapping("/getByKey")
    public ResultEntity getByKey(String paramKey){
        return ResultEntity.success(sysParamService.queryByKey(paramKey));
    }


    /**
     * 查询分页数据接口
     * @param sysParam
     * @return
     */
     @RequiresPermissions("core:param:view")
    @PostMapping("list")
    public ResultEntity list(SysParam sysParam) {
        return ResultEntity.success(sysParamService.page(sysParam));
    }

    /**
     * 根据id物理删除数据接口
     * @param sysParam
     * @return
     */
    @RequiresPermissions("core:param:edit")
    @DeleteMapping("delete")
    public ResultEntity delete(SysParam sysParam) {
        boolean delete = sysParamService.deleteById(sysParam.getId());
        return delete ? ResultEntity.success() : ResultEntity.fail();
    }
    
}
